#include "ffz/raft/raft-table.h"
#include "ffz/raft/raft-role.h"
#include "ffz/ffzconfig.h"
#define DEBUG DEBUG_PRINT
#include "ffz/debug.h"

//the nodes'num
static uint16_t raft_nodes_num;
RaftNodeTableItem items[MAX_NODESLEN];
static uip_ipaddr_t broadcastAddr;

uint16_t raftNodeTableInit(uint16_t num)
{
    int i = 0;
    if (num > MAX_NODESLEN)
    {
        return 0;
    }
    raft_nodes_num = num;
    //initialize the route table
    for (i = 0; i < raft_nodes_num; i++)
    {
        int num = i + 1;
        // uip_ip6addr(&items[i].ip, 0xfe80, 0, 0, 0, 0x0212,

        // platform sky
        // uip_ip6addr(&items[i].ip, 0xaaaa, 0, 0, 0, 0x0212,
        //             0x7400 + num,
        //             num,
        //             num + num * HEX_DOUBLE);

        // platform z1
        // uip_ip6addr(&items[i].ip, 0xfe80, 0, 0, 0, 0xc30c,
        //             0,
        //             0,
        //             num);

        uip_ip6addr(&items[i].ip, 0xaaaa, 0, 0, 0, 0xc30c, 0, 0, num);

        // uip_ip6addr(&items[i].ip, 0xaaaa, 0, 0, 0, 0,
        //             // 0xff00,
        //             0x00ff,
        //             // 0x00fe,
        //             0xfe00,
        //             num);
    }
    // uip_ip6addr(&broadcastAddr, 0xff02, 0, 0, 0, 0, 0, 0, 0x001a);
    uip_ip6addr(&broadcastAddr, 0xff04, 0, 0, 0, 0, 0, 0, 0x0001);
    initRoleVariables();
    return 1;
}

RaftNodeTableItem *getRaftNodeTableItem(uint16_t index)
{
    return &items[index];
}

uint16_t getRaftNodeTableLength()
{
    return raft_nodes_num;
}

uip_ipaddr_t *getBroadcastAddr()
{
    return &broadcastAddr;
}